<?php

namespace app\api\controller\canvas;

use app\admin\model\SketchpadAcc;
use app\api\controller\Base;
use think\View;

/**
 * 魔力画板用户接口
 */
class Sketchpadaccs extends Base
{

    // 登录接口
    public function login()
    {
        $gh_id = $this->request->post('gh_id');
        $code = $this->request->post('code');
        $openid = $this->get_openid($gh_id, $code);

        if ($acc = SketchpadAcc::get(['openid' => $openid])) {
            $data['userid'] = $acc->id;
            $data['level'] = $acc->level;
//            $data['lock'] = 1;
            $data['lock'] = $acc->lock;
        } else {
            $a = new SketchpadAcc;
            $a->openid = $openid;
            $a->save();
            $data['userid'] = $a->id;
            $data['level'] = 0;
//            $data['lock'] = 1;
            $data['lock'] = 0;
        }

        $this->result($openid, $data);
    }

    // 解锁
    public function getLock($openid)
    {
        $acc = SketchpadAcc::get(['openid' => $openid]);
        $acc->lock = 1;
        $acc->save();
    }

    // 配置
    public function config($v)
    {
        $shareflag = true;

        $img_id = rand(1, 5);
        $img = 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/molihuaban/result/' . $img_id . '.jpg';

        if ($v == '0.0.0') {
            $shareflag = false;
            $img = 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/molihuaban/result/0.jpg';
        }

        $flags = array(
            'shareflag' => $shareflag,
        );

        $this->result($img, $flags);
    }

    // 分享回调
    public function share_callback($openid, $type)
    {
        $acc = SketchpadAcc::get(['openid' => $openid]);

        if ($acc->level == 1 || $acc->level == 2) {
            $acc->level = 3;
        } else {
            switch ($type) {
                case 'zxxz':
                    $acc->level = 1;
                    break;
                case 'dcxz':
                    $acc->level = 2;
                    break;
            }
        }

        $acc->save();
        $this->result('level', $acc->level);
    }

    // 上传图片
    public function base64()
    {
        $base64_img = $this->request->post('fileData');

        $base_url = '/uploads/images/' . date('Ymd', time()) . '/';

        //匹配出图片的格式
        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_img, $result)){
            $type = $result[2];
            $new_file = APP_PATH . "/../public" . $base_url;
            if(!file_exists($new_file)){
                //检查是否有该文件夹，如果没有就创建，并给予最高权限
                mkdir($new_file, 0700);
            }
            $file_name = time().".{$type}";
            $new_file = $new_file . $file_name;
            if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_img)))){
                return $this->request->root(true) . $base_url . $file_name;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }

    // 获取web页面
    public function get_web($type, $level)
    {
        $view = new View();

        switch ($type) {
            case 'zyms':
                return $view->fetch('zyms_new');
            case 'zydc':
                return $view->fetch('zydc_new');
            case 'zxxz':
                if ($level == 1 || $level == 3) {
                    return $view->fetch('zxxz_new');
                } else {
                    return $view->fetch('zxxzl_new');
                }
//                return $view->fetch('zxxz_new');
            case 'dcxz':
                if ($level == 2 || $level == 3) {
                    return $view->fetch('dcxz_new');
                } else {
                    return $view->fetch('dcxzl_new');
                }
//                return $view->fetch('dcxz_new');
        }
    }

}
